﻿using HelloOne.NewsSvc.DBLib;
using HelloOne.Repositories;
using HelloOne.Repositories.EF;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace HelloOne.NewsSvc
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            Configuration = configuration;
            Env = env;
        }

        public IConfiguration Configuration { get; }
        public IHostingEnvironment Env { get; }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            IOC(services);
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(x =>
            {
                x.AllowAnyOrigin();
                x.AllowAnyHeader();
                x.AllowAnyMethod();
            });
            app.UseMvc();
        }
        private void IOC(IServiceCollection services)
        {
            string connectStr = Configuration.GetConnectionString(Env.EnvironmentName);
            services.AddDbContext<NewsSvcDBContext>(x => x.UseSqlServer(connectStr));
            //services.AddDbContext<NewsSvcDBContext>(x => x.UseInMemoryDatabase($"NewsSvcDB_{ Env.EnvironmentName}"));
            services.AddTransient<DbContext, NewsSvcDBContext>();
            services.AddTransient<IRepositoryProvider, EFRepositoryProvider>();
        }
    }
}
